package com.example.recyclerviewfenzu.JAC;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.recyclerviewfenzu.R;

import java.util.List;

/**
 * @功能:
 * @Creat 2020/3/14 15:08
 * @User Lmy
 * @Compony JinAnChang
 */
public class RecyclerViewAdapter2 extends RecyclerView.Adapter<RecyclerViewAdapter2.ViewHolder> {
    private Context context;
    private List<DataListBean.DataBean.StateListBean> stateList;

    private RecyclerViewAdapter3 adapter3;

    public RecyclerViewAdapter2(Context context, List<DataListBean.DataBean.StateListBean> stateList) {
        this.context = context;
        this.stateList = stateList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.recyclerview2_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position, List payloads) {
        holder.tvDkrName.setText(stateList.get(position).getDaikanren());
        holder.tvDfState.setText(stateList.get(position).getDaofang());
        holder.tvLoupanName.setText(stateList.get(position).getLoupan());
        holder.tvTime.setText(stateList.get(position).getTime());
        adapter3 = new RecyclerViewAdapter3(context, stateList.get(position).getOther());
        holder.recyclerview3.setLayoutManager(new LinearLayoutManager(context));
        holder.recyclerview3.setNestedScrollingEnabled(false);//禁止滑动
        holder.recyclerview3.setAdapter(adapter3);
        if (stateList.get(position).isIszhankai()) {
            holder.linearLayout.setVisibility(View.VISIBLE);
        } else {
            holder.linearLayout.setVisibility(View.GONE);
        }
        holder.ivPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, stateList.get(position).getPhone(), Toast.LENGTH_SHORT).show();
            }
        });
        holder.clGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (stateList.get(position).isIszhankai()) {
                    stateList.get(position).setIszhankai(false);
                } else {
                    stateList.get(position).setIszhankai(true);
                }
                onItemClickListener.TimeClickListener(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return stateList.size();
    }


    private OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {

        /**
         * 这个接口用于通知最外层adapter的刷新
         */
        void TimeClickListener(int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ConstraintLayout clGroup;
        private TextView tvDfState;
        private TextView tvDaofang;
        private TextView tvDkr;
        private TextView tvDkrName;
        private TextView tvLoupan;
        private TextView tvLoupanName;
        private TextView tvTime;
        private ImageView ivZk;
        private ImageView ivPhone;
        private RecyclerView recyclerview3;
        private LinearLayout linearLayout;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            clGroup = itemView.findViewById(R.id.cl_group);
            tvDfState = itemView.findViewById(R.id.tv_df_state);
            tvDaofang = itemView.findViewById(R.id.tv_daofang);
            tvDkr = itemView.findViewById(R.id.tv_dkr);
            tvDkrName = itemView.findViewById(R.id.tv_dkr_name);
            tvLoupan = itemView.findViewById(R.id.tv_loupan);
            tvLoupanName = itemView.findViewById(R.id.tv_loupan_name);
            tvTime = itemView.findViewById(R.id.tv_time);
            ivZk = itemView.findViewById(R.id.iv_zk);
            ivPhone = itemView.findViewById(R.id.iv_phone);
            recyclerview3 = itemView.findViewById(R.id.recyclerview3);
            linearLayout = itemView.findViewById(R.id.ll_recyclerview);

        }
    }
}
